<!DOCTYPE HTML>
<html lang="zh">
<head>
    <title>My WebSocket</title>
</head>

<body>
<div>1</div>
<div class="send w500">
    <div  id="content"  contenteditable="true"></div>
    <a href="javascript:;" class="btn">发送</a>
    <a href="javascript:;" class='faces'></a>
</div>
<div class="face w500"></div>


<input id="text" type="text" />
<button onclick="send()">Send</button>
<button onclick="closeWebSocket()">Close</button>
<div id="message"></div>
</body>
<script src="/js/jquery.min.js"></script>
<script type="text/javascript">
    var websocket = null;

    //判断当前浏览器是否支持WebSocket, 主要此处要更换为自己的地址
    if ('WebSocket' in window) {
        websocket = new WebSocket("ws://localhost:80/websocket/test/ws/1/2");
    } else {
        alert('Not support websocket')
    }

    //连接发生错误的回调方法
    websocket.onerror = function() {
        setMessageInnerHTML("error");
    };

    //连接成功建立的回调方法
    websocket.onopen = function(event) {
        //setMessageInnerHTML("open");
    }

    //接收到消息的回调方法
    websocket.onmessage = function(event) {
        setMessageInnerHTML(event.data);
    }

    //连接关闭的回调方法
    websocket.onclose = function() {
        setMessageInnerHTML("close");
    }

    //监听窗口关闭事件，当窗口关闭时，主动去关闭websocket连接，防止连接还没断开就关闭窗口，server端会抛异常。
    window.onbeforeunload = function() {
        websocket.close();
    }

    //将消息显示在网页上
    function setMessageInnerHTML(innerHTML) {
        document.getElementById('message').innerHTML += innerHTML + '<br/>';
    }

    //关闭连接
    function closeWebSocket() {
        websocket.close();
    }

    //发送消息
    function send() {
        var message = document.getElementById('text').value;
        websocket.send(message);
    }
</script>
<style>
    *{ margin:0; padding:0; list-style:none;outline-style: none;}
    .send{
        position: relative;
    }
    .w500{
        width: 98%;
        height: auto;
        margin: 0 auto;
        overflow: hidden;
    }
    #content {
        height: 90px;
        border: 1px solid #07C160;
        background-color: #fff;
        border-radius: 5px;
        padding: 3px 10px;
        overflow-y:auto;
    }
    .send a.btn{
        position: absolute;
        right: 20px;
        bottom: 20px;
        padding:5px 20px;
        display: block;
        color: #fff;
        background: #07C160;
        border-radius: 3px;
        text-decoration: none;
        font-size: 14px;
        font-weight: bold;
        float: left;
        margin-top: 5px;
    }
    .send a.btn:hover{
        background: #07C160;
        -webkit-transition:all 0.2s linear;
        -moz-transition:all 0.2s linear;
        -ms-transition:all 0.2s linear;
        -o-transition:all 0.2s linear;
        transition:all 0.2s linear;
    }
    .send .faces{
        position: absolute;
        right: 98px;
        bottom: 20px;
        width: 25px;
        height:25px;
        display: block;
        float: right;
        background-image: url(/img/face/faces.png) ;
        background-position: 0 0;
        margin-right:5px;
        margin-top:5px;
    }
    .send .on{
        background-position: left bottom;
    }
    .face{
        overflow: hidden;
        display: none;}
    .face li{
        width: 22px;
        height: 22px;
        margin:3px;
        float: left;
    }
</style>
</html>